using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ICP_MS.Plugin.AnalyteDb;

public class FormPolyIon : Form
{
	private List<string> atomics = new List<string>();

	private bool isM2 = false;

	private IContainer components = null;

	private Label label1;

	private Label lblIons;

	private Button btnOk;

	private Button btnCancel;

	private Button btnM2;

	private Label label2;

	public List<string> Atomics => atomics;

	public bool IsM2 => isM2;

	public FormPolyIon()
	{
		InitializeComponent();
	}

	private void FormPolyIon_Load(object sender, EventArgs e)
	{
		ApplyResources();
		btnOk.Enabled = false;
		btnM2.Enabled = false;
		btnCancel.Enabled = true;
		base.StartPosition = FormStartPosition.CenterParent;
		Win32API.SetTopMost((Control)this);
	}

	private void ApplyResources()
	{
		Text = Lang.Txt("FormPolyIon.Title.Text");
		label2.Text = Lang.Txt("FormPolyIon.Label1.Text");
		label1.Text = Lang.Txt("FormPolyIon.Label2.Text");
		btnOk.Text = Lang.Txt("FormPolyIon.Button.OK.Text");
		btnCancel.Text = Lang.Txt("FormPolyIon.Button.Cancel.Text");
		btnM2.Text = Lang.Txt("FormPolyIon.Button.M2.Text");
	}

	public void periodicTable_AtomClick(object sender, AtomEventArgs e)
	{
		if (!isM2)
		{
			atomics.Add(e.Atomic);
			if (atomics.Count == 1)
			{
				btnM2.Enabled = true;
			}
			else
			{
				btnM2.Enabled = false;
				btnOk.Enabled = true;
			}
			ShowPolyIons();
		}
	}

	private void ShowPolyIons()
	{
		if (atomics.Count == 1 && isM2)
		{
			lblIons.Text = atomics[0] + "++";
			return;
		}
		StringBuilder sb = new StringBuilder();
		List<string> polyshow = new List<string>();
		IEnumerable<string> ions = atomics.Distinct();
		foreach (string ion in ions)
		{
			sb.Append(ion);
			int count = atomics.Where((string atom) => atom == ion).Count();
			if (count > 2)
			{
				sb.Append(count.ToString());
			}
			sb.Append(" ");
		}
		lblIons.Text = sb.ToString();
	}

	private void btnOk_Click(object sender, EventArgs e)
	{
		base.DialogResult = DialogResult.OK;
		Close();
	}

	private void btnCancel_Click(object sender, EventArgs e)
	{
		base.DialogResult = DialogResult.Cancel;
		Close();
	}

	private void btnM2_Click(object sender, EventArgs e)
	{
		isM2 = true;
		btnOk.Enabled = true;
		btnM2.Enabled = false;
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.label1 = new System.Windows.Forms.Label();
		this.lblIons = new System.Windows.Forms.Label();
		this.btnOk = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.btnM2 = new System.Windows.Forms.Button();
		this.label2 = new System.Windows.Forms.Label();
		base.SuspendLayout();
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(113, 43);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(35, 12);
		this.label1.TabIndex = 0;
		this.label1.Text = "元素:";
		this.lblIons.Location = new System.Drawing.Point(154, 32);
		this.lblIons.Name = "lblIons";
		this.lblIons.Size = new System.Drawing.Size(139, 23);
		this.lblIons.TabIndex = 1;
		this.btnOk.Enabled = false;
		this.btnOk.Location = new System.Drawing.Point(35, 83);
		this.btnOk.Name = "btnOk";
		this.btnOk.Size = new System.Drawing.Size(75, 23);
		this.btnOk.TabIndex = 2;
		this.btnOk.Text = "确定";
		this.btnOk.UseVisualStyleBackColor = true;
		this.btnOk.Click += new System.EventHandler(btnOk_Click);
		this.btnCancel.Enabled = false;
		this.btnCancel.Location = new System.Drawing.Point(132, 83);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 3;
		this.btnCancel.Text = "取消";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.btnM2.Enabled = false;
		this.btnM2.Location = new System.Drawing.Point(231, 83);
		this.btnM2.Name = "btnM2";
		this.btnM2.Size = new System.Drawing.Size(75, 23);
		this.btnM2.TabIndex = 4;
		this.btnM2.Text = "M2+";
		this.btnM2.UseVisualStyleBackColor = true;
		this.btnM2.Click += new System.EventHandler(btnM2_Click);
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(12, 9);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(149, 12);
		this.label2.TabIndex = 5;
		this.label2.Text = "请从元素周期表选择元素：";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(349, 143);
		base.Controls.Add(this.label2);
		base.Controls.Add(this.btnM2);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.btnOk);
		base.Controls.Add(this.lblIons);
		base.Controls.Add(this.label1);
		base.Name = "FormPolyIon";
		base.ShowIcon = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "干扰元素编辑";
		base.TopMost = true;
		base.Load += new System.EventHandler(FormPolyIon_Load);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
